package com.iespring.config.shiro;
import com.iespring.bean.admin.user.entity.*;
import com.iespring.mapper.admin.MallAdminMapper;
import com.iespring.mapper.admin.MallPermissionMapper;
import com.iespring.mapper.admin.MallUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * shiro的Realm
 * 通过realm来实现后台管理和小程序的认证和授权
 */
@Component
public class MarketRealm extends AuthorizingRealm {

    @Resource
    private MallAdminMapper adminMapper;

    @Resource
    private MallUserMapper userMapper;

    @Resource
    private MallPermissionMapper permissionMapper;

    /**
     * Shrio认证信息
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String type = ((MallToken) authenticationToken).getType();
        String username= (String) authenticationToken.getPrincipal();

        if("admin".equals(type)){
            //sql拼接：根据用户名查询数据库对应的管理员信息
            MallAdminExample adminExample = new MallAdminExample();
            adminExample.createCriteria().andUsernameEqualTo(username);
            List<MallAdmin> mallAdmins = adminMapper.selectByExample(adminExample);
            //TODO
            if (mallAdmins.size() == 1){
                MallAdmin mallAdmin = mallAdmins.get(0);
                return new SimpleAuthenticationInfo(mallAdmin,mallAdmin.getPassword(),getName());
            }

        }else if("wx".equals(type)){
            MallUserExample userExample = new MallUserExample();
            MallUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<MallUser> mallUsers = userMapper.selectByExample(userExample);
            if(mallUsers.size()==1){
                MallUser mallUser = mallUsers.get(0);
                return new SimpleAuthenticationInfo(mallUser,mallUser.getPassword(),getName());
            }
        }
        return null;
    }


    /**
     * shiro的授权信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        MallAdmin principal = (MallAdmin) principalCollection.getPrimaryPrincipal();

        //根据用户信息里的role-id,到permission表中拿到permission信息
        List<String> mallPermissions = null;
        for (Integer roleId : principal.getRoleIds()) {
            MallPermissionExample permissionExample = new MallPermissionExample();
            permissionExample.createCriteria().andRoleIdEqualTo(roleId);
            mallPermissions = permissionMapper.selectPermissionsByExample(permissionExample);
        }
        //把权限信息放入授权信息中
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(mallPermissions);
        return simpleAuthorizationInfo;
    }
}
